package com.coin900.coin.cmd.capital;

import com.coin900.coin.base.BxConstants;
import com.coin900.coin.base.CoinConstants;
import com.coin900.coin.base.FuConstants;
import com.coin900.coin.base.restTemplate.BasicAuthRestTemplate;
import com.coin900.coin.base.utils.NumberArithmeticUtils;
import com.coin900.coin.cmd.ACmd;
import com.coin900.coin.cmd.CmdSign;
import com.coin900.coin.code.CmdCode;
import com.coin900.coin.code.RespCode;
import com.coin900.coin.persist.coin.CoinCoinVO;
import com.coin900.coin.persist.fu.FuCapitalDetailVO;
import com.coin900.coin.persist.fu.FuCapitalWithdrawVO;
import com.coin900.coin.service.coin.ICoinCoinService;
import com.coin900.coin.service.fu.IFuCapitalDetailService;
import com.coin900.coin.service.fu.IFuCapitalWithdrawService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 正式提现操作
 *
 * @author shui
 * @create 2018-01-31
 **/
@Controller
public class FuCapitalWithdrawSendToAddressCmd extends ACmd {
    @Override
    public CmdSign execute(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        String source = sign.getSource();
        JSONObject jsonObject = JSONObject.fromObject(source);
        long id = jsonObject.getLong("id");

        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        params.put("dataStatus", 1);
        List<FuCapitalWithdrawVO> fuCapitalWithdrawVOS = this.withdrawService.loadFuCapitalWithdraw(params);
        if (fuCapitalWithdrawVOS.isEmpty()) {
            respSign.setRespCode(RespCode.FAIL);
            respSign.setMsg("提现申请不存在");
            return respSign;
        }
        FuCapitalWithdrawVO fuCapitalWithdrawVO = fuCapitalWithdrawVOS.get(0);
        Integer checkStatus = fuCapitalWithdrawVO.getCheckStatus();
        if (!new Integer(FuConstants.FU_CHECK_STATUS_SUCCESS).equals(checkStatus)) {
            respSign.setRespCode(RespCode.FAIL);
            respSign.setMsg("提现审核未通过");
            return respSign;
        }

        Long coinId = fuCapitalWithdrawVO.getCoinId();
        CoinCoinVO coinVO = this.coinService.getCoin(coinId);

        String address = fuCapitalWithdrawVO.getAddress();
        String currency = coinVO.getCoinName();

        // 精度是几位
        Integer accuracy = coinVO.getAccuracy();
        Double balance = fuCapitalWithdrawVO.getBalance();
        BigDecimal bigDecimal = NumberArithmeticUtils.safeMultiply(Math.pow(10, accuracy), balance).setScale(0);
        // balance 要根据类型格式化：精度 8 位 18 位
        String amount = bigDecimal.toString();
        String url = BxConstants.BX_BLOCK_CHAIN_SEND_TO_ADDRESS + "?to=" +
                address + "&currency=" + currency + "&amount=" + amount;
        BasicAuthRestTemplate restTemplate = new BasicAuthRestTemplate(BxConstants.BX_BLOCK_CHAIN_BASIC_NAME, BxConstants.BX_BLOCK_CHAIN_BASIC_PASS);
        ResponseEntity<String> result = restTemplate.getForEntity(url + "&address=" + address, String.class);
        String txid = result.getBody();

        // 插入 txid
        params.clear();
        params.put("type", 2);
        params.put("targetId", fuCapitalWithdrawVO.getId());
        params.put("dataStatus", 1);
        List<FuCapitalDetailVO> fuCapitalDetailVOS = this.detailService.loadFuCapitalDetail(params);
        FuCapitalDetailVO dbCapitalDetailVO = fuCapitalDetailVOS.get(0);
        FuCapitalDetailVO fuCapitalDetailVO = new FuCapitalDetailVO();
        fuCapitalDetailVO.setId(dbCapitalDetailVO.getId());
        fuCapitalDetailVO.setHash(txid);
        fuCapitalDetailVO.setModifiedInfor(getUser(sign));
        fuCapitalDetailVO.setTransferStatus(CoinConstants.COIN_CAPITAL_CONFIRM_WATIONG);
        this.detailService.doCapitalWithdrawSendToAddress(fuCapitalDetailVO);

        return respSign;
    }

    @Autowired
    private IFuCapitalWithdrawService withdrawService;
    @Autowired
    private IFuCapitalDetailService detailService;
    @Autowired
    private ICoinCoinService coinService;

    @Override
    public String getCmdCode() {
        return CmdCode.FU_CAPITAL_WITHDRAW_SEND_TO_ADDRESS;
    }
}
